{{ extend '../_layouts/home.html' }}

{{ block 'title' }}{{ '修改密码' }}{{ /block }}

{{ block 'body' }} 
<section class="container">
    {{ include '../_partials/setting-avatar.html' }}
    <div class="col-md-9">
        <div class="list-container">
            <ul class="list-tab">
                <li><a href="/settings/profile">个人主页</a></li>
                <li><a href="/myFollow">关注</a></li>
                <li><a href="/myFans">粉丝</a></li>
                <li><a href="/myCollection">收藏</a></li>
                <li><a href="/settings/admin" class="active">修改密码</a></li>
                <li><a href="/settings/createdTopics">已创建话题</a></li>
                <li><a href="/settings/partTopics">已参与话题</a></li>
            </ul>    
        </div>
        <div class="subhead">
            <h2>修改密码</h2>
        </div>
        <hr>
        <form action="" class="form-horizontal" id="pwd_form">
            <div class="form-group">
                <label for="password" class="col-sm-2 control-label">当前密码</label>
                <div class="col-sm-7">
                    <input type="hidden" name="id" id="id" value="{{ user._id }}">
                    <input type="password" class="form-control" id="password" placeholder="" name="password">
                </div>
                <div class="col-sm-3">
                    <p class="form-control-static" id="tipOne">
                    </p>
                </div>
            </div>
            <div class="form-group">
                <label for="newPwd" class="col-sm-2 control-label">新的密码</label>
                <div class="col-sm-7">
                    <input type="password" class="form-control" id="newPwd" placeholder="" name="newPwd">
                </div>
            </div>
            <div class="form-group">
                <label for="comformPwd" class="col-sm-2 control-label">确认密码</label>
                <div class="col-sm-7">
                    <input type="password" class="form-control" id="comformPwd" placeholder="" name="comformPwd">
                </div>
            </div>
            <div class="form-group">
                <label for="" class="col-sm-2 control-label"></label>
                <div class="col-sm-7">
                    <button id="postPwd" type="submit" class="btn btn-success">提交</button>
                    <button type="reset" class="btn btn-default">重置</button>
                </div>
            </div>
        </form>
    </div>
</section>
{{ /block }}
{{ block 'script' }} 
<script>
$(function () {
    $('#pwd_form').on('submit', function (e) {
        e.preventDefault()
    })
    $('#pwd_form').validate({
        rules: {
            password: {
                required: true
            },
            newPwd: {
                required: true,
                rangelength: [6, 12]
            },
            comformPwd: {
                required: true,
                rangelength: [6, 12],
                equalTo: '#newPwd'
            }
        },
        messages: {
            password: {
                required: '请输入您当前密码'
            },
            newPwd: {
                required: '请输入新密码',
                rangelength: '请输入 6-12 位密码'
            },
            comformPwd: {
                required: '请您确认密码',
                rangelength: '请输入 6-12 位密码',
                equalTo: '两次密码输入不一致'
            }
        }
    })
    $('#password').blur(function(){
        $.ajax({
            url: '/findPwd',
            type: 'post',
            data: {
                id: $('#id').val(),
                password: $(this).val()
            },
            dataType: 'json',
            success: function (data) {
                var err_code = data.err_code
                if (err_code === 0) {
                    $('#tipOne').html('<span class="greenColor"><span class="glyphicon glyphicon-ok-circle"></span> 恭喜您，密码正确</span>')
                } else if (err_code === 1) {
                    $('#tipOne').html('<span class="redColor"><span class="glyphicon glyphicon-remove-circle"></span> 密码错误，请重新输入</span> ')
                }
            }
        })
    })
    $('#postPwd').click(function () {
        $.ajax({
            url: '/updatePwd',
            type: 'post',
            data: {
                id: $('#id').val(),
                password: $('#comformPwd').val()
            },
            dataType: 'json',
            success: function (data) {
                var err_code = data.err_code
                if (err_code === 0) {
                    window.alert(data.message)
                    window.location.href = '/login'
                }
            }
        })
    })
})
</script>
{{ /block }}